<?php 
class DomArDatesTestCase extends DomArTestUnitTestCase {

	public function testDomDateTime() {
		

		$item = new DomArTestClass_DomDates('a');
		$this->assertEqual( $item->access('datetime'), NULL );
		$this->assertEqual( get_class($item->datetime), 'DomArDateTime');
		
		$stamp = strtotime('-6 days');
		$datetime = date(DOMAR_DATETIME_DEFAULT_FORMAT, $stamp );
		
		// testime kuupäeva andmist stambina.
		$item->datetime = $stamp;
		$this->assertEqual( get_class($item->datetime), 'DomArDateTime');
		$this->assertEqual( $item->datetime->stamp, $stamp);
		$this->assertEqual( $item->datetime->datetime, $datetime);
		$this->assertTrue( $item->isChanged('datetime') );
		
		
		// testime kuupäeva andmist objektina
		$item->datetime = new DomArDateTime($stamp);
		$this->assertEqual( get_class($item->datetime), 'DomArDateTime');
		$this->assertEqual( $item->datetime->stamp, $stamp);
		$this->assertEqual( $item->datetime->datetime, $datetime);
		$this->assertTrue( $item->isChanged('datetime') );
		
	}
	
	public function testDomDateTimeNull() {
		DomArTestClass_DomDates::getTable()->update();
		
		$item = new DomArTestClass_DomDates('a');
		$this->assertTrue( $item->nullDate->isNull() );
		$this->assertTrue( $item->nullDatetime->isNull() );
		
		$item->datetime = 0;
		$this->assertTrue( $item->datetime->isNull() );
		
		$item->datetime = '0000-00-00 00:00:00';


		$this->assertIdentical( $item->datetime->stamp(), 0);
		
		$this->assertIdentical( $item->datetime->stamp, 0);
		$this->assertIdentical( $item->datetime->datetime(), '0000-00-00 00:00:00');
		$this->assertFalse( $item->datetime->isNull() );
		
		$item->date = '0000-00-00';
		$this->assertIdentical( $item->date->stamp(), 0);
		$this->assertIdentical( $item->date->stamp, 0);
		$this->assertIdentical( $item->date->date(), '0000-00-00');
		$this->assertFalse( $item->date->isNull() );
		
		$item->save();
		
		DomAr::flushStoredObjects();
		
		$nItem = DomArTestClass_DomDates::load( $item->id );
		
		$this->assertIdentical( $item->datetime->stamp(), 0);
		$this->assertIdentical( $item->datetime->stamp, 0);
		$this->assertIdentical( $item->datetime->datetime(), '0000-00-00 00:00:00');
		$this->assertFalse( $item->datetime->isNull() );
		
		$this->assertIdentical( $item->date->stamp(), 0);
		$this->assertIdentical( $item->date->stamp, 0);
		$this->assertIdentical( $item->date->date(), '0000-00-00');
		$this->assertFalse( $item->date->isNull() );
		
		
		$this->assertTrue( $item->nullDate->isNull() );
		$this->assertTrue( $item->nullDatetime->isNull() );
	}
	
	public function testDomDateTime_ToParts() {

		$time = new DomArDateTime();
		
		$parsed  = $time->parseDateTime('2008-10-11 12:13:14');
		$this->assertEqual( $parsed['year'], '2008');
		$this->assertEqual( $parsed['month'], '10');
		$this->assertEqual( $parsed['day'], '11');
		$this->assertEqual( $parsed['hour'], '12');
		$this->assertEqual( $parsed['minute'], '13');
		$this->assertEqual( $parsed['second'], '14');
		
		$parsed  = $time->parseDateTime('208-4-1 2:3:4');
		$this->assertEqual( $parsed['year'], '0208');
		$this->assertEqual( $parsed['month'], '04');
		$this->assertEqual( $parsed['day'], '01');
		$this->assertEqual( $parsed['hour'], '02');
		$this->assertEqual( $parsed['minute'], '03');
		$this->assertEqual( $parsed['second'], '04');
		
		$parsed  = $time->parseDateTime('90-4-1 2:3:4');
		$this->assertEqual( $parsed['year'], '0090');
		$this->assertEqual( $parsed['month'], '04');
		$this->assertEqual( $parsed['day'], '01');
		$this->assertEqual( $parsed['hour'], '02');
		$this->assertEqual( $parsed['minute'], '03');
		$this->assertEqual( $parsed['second'], '04');
	
	}
	
	public function testDomDateTime_Validate() {
	
		$time = new DomArDateTime();
		
		$this->assertFalse( $time->validate('2008-14-25 10:11:12') );
		$this->assertFalse( $time->validate('2008-10-35 10:11:12') );
		$this->assertFalse( $time->validate('2008-10-25 24:11:12') );
		$this->assertFalse( $time->validate('2008-10-25 10:60:12') );
		$this->assertFalse( $time->validate('2008-10-25 10:11:60') );
		
		$this->assertTrue( $time->validate('2008-11-25 10:11:12') );
		$this->assertTrue( $time->validate('8-11-25 10:11:12') );
		$this->assertTrue( $time->validate('28-11-25 10:11:12') );
		$this->assertTrue( $time->validate('208-11-25 10:11:12') );
		$this->assertTrue( $time->validate('2008-1-25 10:11:12') );
		$this->assertTrue( $time->validate('2008-11-5 10:11:12') );
		$this->assertTrue( $time->validate('2008-11-25 1:11:12') );
		$this->assertTrue( $time->validate('2008-11-25 10:1:2') );
		$this->assertTrue( $time->validate('8-1-5 1:1:2') );
		
	}
	
	public function testDomDateTime_MiscTests() {
	
		return;
		
		$this->assertEqual( DOMAR_DATETIME_DEFAULT_FORMAT, 'Y-m-d H:i:s', 'Test date format is Y-m-d H:i:s, but config default format is '.DOMAR_DATETIME_DEFAULT_FORMAT.'. This case can\'t pass if format is not same!');
		
		$stamp1 = strtotime('now');
		$datetime1 = date('Y-m-d H:i:s', $stamp1);
		$date1 = date('Y-m-d', $stamp1);
		$time1 = date('H:i:s', $stamp1);
		
		// testime DomArDateTime meetodite õigsust kui anname aja stambina.
		$time = new DomArDateTime();
		$time->stamp = $stamp1;
		$this->assertEqual( $time->stamp, $stamp1 );
		$this->assertEqual( $time->datetime, $datetime1 );
		$this->assertEqual( $time->datetime(), $datetime1 );
		$this->assertEqual( $time->date(), $date1 );
		$this->assertEqual( $time->time(), $time1 );
		
		// testime DomArDateTime meetodite õigsust kui anname aja kuupäevana.
		$time->datetime = $datetime1;
		$this->assertEqual( $time->stamp, $stamp1 );
		$this->assertEqual( $time->datetime, $datetime1 );
		$this->assertEqual( $time->datetime(), $datetime1 );
		$this->assertEqual( $time->date(), $date1 );
		$this->assertEqual( $time->time(), $time1 );
		
		
		// testime time past meetodeid
		$r = DomArDateTime::timePastElements( strtotime('-5 years'), array('Y'));
		$this->assertEqual( $r['Y'], 5);
		$r = DomArDateTime::timePastElements( strtotime('-5 months'), array('m'));
		$this->assertEqual( $r['m'], 5);
		$r = DomArDateTime::timePastElements( strtotime('-5 days'), array('d'));
		$this->assertEqual( $r['d'], 5);
		$r = DomArDateTime::timePastElements( strtotime('-5 hours'), array('H'));
		$this->assertEqual( $r['H'], 5);
		$r = DomArDateTime::timePastElements( strtotime('-5 minutes'), array('i'));
		$this->assertEqual( $r['i'], 5);
		$r = DomArDateTime::timePastElements( strtotime('-5 seconds'), array('s'));
		$this->assertEqual( $r['s'], 5);
		
		$add = strtotime('now') + (3600*24*365)*2 + (3600*24*30)*3 + (3600*24)*4 + (3600)*5 + (60)*6 + 7;
		$r = DomArDateTime::timePastElements( $add, array('Y','m','d','H','i','s'));
		$this->assertEqual( $r['Y'], 2);
		$this->assertEqual( $r['m'], 3);
		$this->assertEqual( $r['d'], 4);
		$this->assertEqual( $r['H'], 5);
		$this->assertEqual( $r['i'], 6);
		$this->assertEqual( $r['s'], 7);
		
		
		// testime time past to string meetodit
		$r = DomArDateTime::timePastToString( strtotime('-5 years'), array('Y'));
		$this->assertEqual( $r, '5 aastat');
		$r = DomArDateTime::timePastToString( strtotime('-5 months'), array('m'));
		$this->assertEqual( $r, '5 kuud');
		$r = DomArDateTime::timePastToString( strtotime('-5 days'), array('d'));
		$this->assertEqual( $r, '5 päeva');
		$r = DomArDateTime::timePastToString( strtotime('-5 hours'), array('H'));
		$this->assertEqual( $r, '5 tundi');
		$r = DomArDateTime::timePastToString( strtotime('-5 minutes'), array('i'));
		$this->assertEqual( $r, '5 minutit');
		$r = DomArDateTime::timePastToString( strtotime('-5 seconds'), array('s'));
		$this->assertEqual( $r, '5 sekundit');
		
		
		$r = DomArDateTime::timePastToString( $add, array('Y','m','d','H','i','s'));
		$this->assertEqual( $r, '2 aastat 3 kuud 4 päeva 5 tundi 6 minutit 7 sekundit');
		
		
		
	}
	
	public function testDateTimeCompare() {
		
		$time = strtotime('now')-10;
		$date = date('Y-m-d H:i:s', $time );
		$obTime = new  DomArDateTime( $time );
		$obDate = new  DomArDateTime( $date );
		
		$item = new DomArTestClass_DomDates('a');
		$item->datetime  = $time;

		$this->assertTrue( $item->isChanged('datetime') );
		$item->setChanged('datetime',false);
		
		$item->datetime = $time;
		$this->assertFalse( $item->isChanged('datetime') );
		
		$item->datetime = $obTime;
		$this->assertFalse( $item->isChanged('datetime') );
		
		$item->datetime = $obDate;
		$this->assertFalse( $item->isChanged('datetime') );

	}
	
	
	
	
	
	public function testDate() {
		
		$item = new DomArTestClass_DomDates('a');
		$this->assertEqual( $item->access('date'), NULL );
		$this->assertEqual( get_class($item->date), 'DomArDate');
		
		$stamp = mktime(0,0,0,10,11,1990);
		$date = date(DOMAR_DATE_DEFAULT_FORMAT, $stamp );
		
		// testime kuupäeva andmist stambina.
		$item->date = $stamp;
		$this->assertEqual( get_class($item->date), 'DomArDate');
		$this->assertEqual( $item->date->stamp, $stamp);
		$this->assertEqual( $item->date->date, $date);
		$this->assertTrue( $item->isChanged('date') );
		
		
		// testime kuupäeva andmist objektina
		$item->date = new DomArDate($stamp);
		$this->assertEqual( get_class($item->date), 'DomArDate');
		$this->assertEqual( $item->date->stamp, $stamp);
		$this->assertEqual( $item->date->date, $date);
		$this->assertTrue( $item->isChanged('date') );
		
	}
	
	public function testDate_ToParts() {
		
		$time = new DomArDate();
		
		$parsed  = $time->parseDate('2008-10-11');
		$this->assertEqual( $parsed['year'], '2008');
		$this->assertEqual( $parsed['month'], '10');
		$this->assertEqual( $parsed['day'], '11');
		
		$parsed  = $time->parseDate('208-4-1');
		$this->assertEqual( $parsed['year'], '0208');
		$this->assertEqual( $parsed['month'], '04');
		$this->assertEqual( $parsed['day'], '01');
		
		$parsed  = $time->parseDate('90-4-1');
		$this->assertEqual( $parsed['year'], '0090');
		$this->assertEqual( $parsed['month'], '04');
		$this->assertEqual( $parsed['day'], '01');
		
	}
	
	public function testDate_Validate() {
		
		$time = new DomArDate();
		
		$this->assertFalse( $time->validate('2008-14-25') );
		$this->assertFalse( $time->validate('2008-10-35') );
		
		$this->assertTrue( $time->validate('2008-11-25') );
		$this->assertTrue( $time->validate('8-11-25') );
		$this->assertTrue( $time->validate('28-11-25') );
		$this->assertTrue( $time->validate('208-11-25') );
		$this->assertTrue( $time->validate('2008-1-25') );
		$this->assertTrue( $time->validate('2008-11-5') );
		$this->assertTrue( $time->validate('8-1-5') );
		
	}
	
	public function testDate_MiscTests() {
			
		return;
		
		$this->assertEqual( DOMAR_DATE_DEFAULT_FORMAT, 'Y-m-d', 'Test date format is Y-m-d, but config default format is '.DOMAR_DATE_DEFAULT_FORMAT.'. This case can\'t pass if format is not same!');
		
		
		$date1 = date('Y-m-d');
		$stamp1 = mktime( 0, 0, 0, date('m'), date('d'), date('Y') );
		
		// testime DomArDate meetodite õigsust kui anname aja stambina.
		$time = new DomArDate();
		$time->stamp = $stamp1;
		$this->assertEqual( $time->stamp, $stamp1 );
		$this->assertEqual( $time->date, $date1 );
		$this->assertEqual( $time->date(), $date1 );

		// testime DomArDate meetodite õigsust kui anname aja kuupäevana.
		$time->date = $date1;
		$this->assertEqual( $time->stamp, $stamp1 );
		$this->assertEqual( $time->date, $date1 );
		$this->assertEqual( $time->date(), $date1 );
		
		
		// testime time past meetodeid
		$r = DomArDate::timePastElements( strtotime('-5 years'), array('Y'));
		$this->assertEqual( $r['Y'], 5);
		$r = DomArDate::timePastElements( strtotime('-5 months'), array('m'));
		$this->assertEqual( $r['m'], 5);
		$r = DomArDate::timePastElements( strtotime('-5 days'), array('d'));
		$this->assertEqual( $r['d'], 5);

		
		$add = strtotime('now') + (3600*24*365)*2 + (3600*24*30)*3 + (3600*24)*4;
		$r = DomArDate::timePastElements( $add, array('Y','m','d'));
		$this->assertEqual( $r['Y'], 2);
		$this->assertEqual( $r['m'], 3);
		$this->assertEqual( $r['d'], 4);
		
		
		// testime time past to string meetodit
		$r = DomArDate::timePastToString( strtotime('-5 years'), array('Y'));
		$this->assertEqual( $r, '5 aastat');
		$r = DomArDate::timePastToString( strtotime('-5 months'), array('m'));
		$this->assertEqual( $r, '5 kuud');
		$r = DomArDate::timePastToString( strtotime('-5 days'), array('d'));
		$this->assertEqual( $r, '5 päeva');
		
		
		$r = DomArDate::timePastToString( $add, array('Y','m','d'));
		$this->assertEqual( $r, '2 aastat 3 kuud 4 päeva');
		
		
		
	}
	
	public function testSave() {
		DomArTestClass_DomDates::getTable()->update();
		
		$item = new DomArTestClass_DomDates('a');
		$item->save();
		$this->assertObjectDatabaseValue( $item, 'nullDate', NULL );
		$this->assertObjectDatabaseValue( $item, 'nullDatetime', NULL );
		$this->assertTrue( $item->date->dateNum() > 0 );
		$this->assertTrue( $item->datetime->dateTimeNum() > 0 );
		
		$item->date = '2008-10-25';
		$item->datetime = '2008-10-25 10:11:12';
		
		$this->assertEqual( $item->date->date(), '2008-10-25');
		$this->assertEqual( $item->datetime->dateTime(), '2008-10-25 10:11:12');
		
		$item->save();

		$this->assertObjectExist( $item );
		$this->assertObjectDatabaseValue( $item, 'date', '2008-10-25' );
		$this->assertObjectDatabaseValue( $item, 'datetime', '2008-10-25 10:11:12');
		
		$item->delete();
		$this->assertObjectNotExist( $item );
		$this->assertTableEmpty( $item->getTableName() );
		
	}
	
	public function testDateCompare() {
		
		$time = strtotime('now')-100000; // take old date, not today
		$date = date('Y-m-d', $time );
		$obTime = new  DomArDate( $time );
		$obDate = new  DomArDate( $date );
		
		$item = new DomArTestClass_DomDates('a');
		$item->date  = $time;
		$this->assertTrue( $item->isChanged('date') );
		$item->setChanged('date',false);
		
		$item->date = $time;
		$this->assertFalse( $item->isChanged('date') );
		
		$item->date = $obTime;
		$this->assertFalse( $item->isChanged('date') );
		
		$item->date = $obDate;
		$this->assertFalse( $item->isChanged('date') );
		
		
		// test setting only year
		$obTime = new  DomArDate( '2008' );
		
		$item = new DomArTestClass_DomDates('a');
		$item->date  = '2008';
		$this->assertTrue( $item->isChanged('date') );
		$item->setChanged('date',false);
		
		$item->date = '2008';
		$this->assertFalse( $item->isChanged('date') );

	}

}
?>